﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernateDB;

namespace MedicalDB
{
    public partial class AdaugareFisaExternare : UserControl
    {
        private Control redControl;
        string CNP;
        string limba;
        Dictionary<string,string> namesDANU;

        public AdaugareFisaExternare(string limba,string CNP)
        {
            InitializeComponent();
            Rename(limba);

            string xmlFile = @"C:\Language" + limba + ".xml";
            XMLParser xmlParser = new XMLParser(xmlFile);
            List<string> namesForm = xmlParser.ExtractAttribute(cbStarePacient.Name, string.Empty, string.Empty, "name");
            namesDANU = xmlParser.ExtractAttributePairs("DANU", string.Empty, string.Empty, "name", "formula");

            foreach (string s in namesForm)
                cbStarePacient.Items.Add(s);

            this.CNP = CNP;
            this.limba = limba;
            tbMotivDeces.Enabled = false;
        }

        private void btAdaugaFisaExternare_Click(object sender, EventArgs e)
        {
            if (redControl != null)
                changeColor(redControl);

            FisaExternare fe = new FisaExternare();

            fe.IdPacient = CNP;

            if(cbStarePacient.SelectedIndex == 0)
            {
                fe.Vindecat = namesDANU["DA"];
                fe.Ameliorat = namesDANU["NU"];
                fe.Transferat = namesDANU["NU"];
                fe.Agravat = namesDANU["NU"];
                fe.Decedat = namesDANU["NU"];
            }
            else if (cbStarePacient.SelectedIndex == 1)
            {
                fe.Vindecat = namesDANU["NU"];
                fe.Ameliorat = namesDANU["DA"];
                fe.Transferat = namesDANU["NU"];
                fe.Agravat = namesDANU["NU"];
                fe.Decedat = namesDANU["NU"];
            }
            else if (cbStarePacient.SelectedIndex == 2)
            {
                fe.Vindecat = namesDANU["NU"];
                fe.Ameliorat = namesDANU["NU"];
                fe.Transferat = namesDANU["DA"];
                fe.Agravat = namesDANU["NU"];
                fe.Decedat = namesDANU["NU"];
            }
            else if(cbStarePacient.SelectedIndex ==3)
            {
                fe.Vindecat = namesDANU["NU"];
                fe.Ameliorat = namesDANU["NU"];
                fe.Transferat = namesDANU["NU"];
                fe.Agravat = namesDANU["DA"];
                fe.Decedat = namesDANU["NU"];
            }
            else if (cbStarePacient.SelectedIndex == 4)
            {
                fe.Vindecat = namesDANU["NU"];
                fe.Ameliorat = namesDANU["NU"];
                fe.Transferat = namesDANU["NU"];
                fe.Agravat = namesDANU["NU"];
                fe.Decedat = namesDANU["DA"];
            }
           
            
            fe.MotivDeces = tbMotivDeces.Text;
            fe.AlteStari = tbAlteStari.Text;
            fe.DiagnosticExternare  = tbDiagnosticExternare.Text;
            fe.RecomandariEpicriza = tbRecomandariEpicriza.Text;
            fe.PerioadaInternarii = tbPerioadaInternarii.Text;
            fe.Concediu = tbConcediu.Text;
            fe.Control = tbControl.Text;
            fe.Tratament = tbTratament.Text;
            fe.Data = dtpData.Value;
            fe.Semnatura = tbSemnatura.Text;

            List<Control> toValidate = new List<Control>();
            bool isValid = true;

            // Validare AdaugareFisaExternare;
            toValidate.Add(tbMotivDeces);
            toValidate.Add(tbAlteStari);
            toValidate.Add(tbDiagnosticExternare);
            toValidate.Add(tbRecomandariEpicriza);
            toValidate.Add(tbPerioadaInternarii);
            toValidate.Add(tbConcediu);
            toValidate.Add(tbControl);
            toValidate.Add(tbTratament);
            toValidate.Add(dtpData);
            toValidate.Add(tbSemnatura);

            string xmlFile = @"C:\Language" + limba + ".xml";
            XMLParser xmlParser = new XMLParser(xmlFile);
            Dictionary<string, string> errors = xmlParser.ExtractAttributePairs("MesajeEroare", string.Empty, string.Empty, "name", "formula");


            foreach (Control tb in toValidate)
            {
                Validation val = new Validation(tb, tb.Text);
                isValid = val.validate();

                if (isValid == false)
                {
                    string eroare = val.getError();
                    redControl = tb;
                    MessageBox.Show(errors["CampIncorect"], "Error",
                      MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                }
            }

            //salvare in BD
           
            if (isValid == true)
            {
                try
                {
                    PersistenceManager pm = PersistenceManager.Instance;
                    pm.Save(fe);
                    MessageBox.Show(errors["okBD"], "Information",
                           MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MessageBox.Show(errors["ErrorBD"], "Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            
        }

        private void changeColor(Control cont)
        {
            if (cont.GetType() == typeof(TextBox))
            {
                cont.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Window);
            }
            if (cont.GetType() == typeof(DateTimePicker))
            {
                cont.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Window);
            }
            if (cont.GetType() == typeof(ComboBox))
            {
                cont.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Window);
            }
        }

        private void cbStarePacient_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbStarePacient.SelectedIndex == 4)
                tbMotivDeces.Enabled = true;
        }
    }
}
